package com.homeinns.lyx.server.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.homeinns.lyx.server.dataobject.OrderDetail;
import com.homeinns.lyx.server.dto.OrderDTO;
import com.homeinns.lyx.server.form.OrderForm;

import java.util.ArrayList;
import java.util.List;

public class OrderForm2OrderDTOConverter{

    public static OrderDTO convert(OrderForm orderForm){

        Gson gson=new Gson();
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        List<OrderDetail> orderDetails = new ArrayList<>();

        orderDTO.setOrderDetails(orderDetails);
        try{
            orderDetails = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){

        }
        return orderDTO;
    }
}
